#include <func.h>
#include "net_pan.h"
void cleanmutex(void *arg){
    net_pan_t * net_pan = (net_pan_t *)arg;
    pthread_mutex_unlock(&net_pan->taskQueue.mutex);
}
void *threadFunc(void *arg){
    net_pan_t * net_pan = (net_pan_t *)arg;
    while(1){
        int netfd;
        struct sockaddr_in *netaddr;
        // 准备取出任务
        pthread_mutex_lock(&net_pan->taskQueue.mutex);
        while(net_pan->exitFlag == 0 &&  net_pan->taskQueue.queueSize == 0){
            // 队列为空，子线程陷入等待
            pthread_cond_wait(&net_pan->taskQueue.cond, &net_pan->taskQueue.mutex);
        }
        if(net_pan->exitFlag != 0){
            printf("child thread is going to exit!\n");
            pthread_mutex_unlock(&net_pan->taskQueue.mutex);
            pthread_exit(NULL);
        }
        printf("worker thread get task!\n");
        netfd = net_pan->taskQueue.pFront->pNext->netfd; // get queue topi
        netaddr=net_pan->taskQueue.pFront->pNext->netaddr;
        taskDeQueue(&net_pan->taskQueue);
        pthread_mutex_unlock(&net_pan->taskQueue.mutex);
        work(netfd,netaddr);
    }
}

int makeWorker(net_pan_t *net_pan){
    for(int i = 0; i < net_pan->threadnum; ++i){
        pthread_create(&net_pan->tidArr[i],NULL,threadFunc,(void *)net_pan);
    }
    return 0;
}



